home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / Ada95 / samples / ncurses2-getch_test.adb < prev    next >
Text File  |  2002-10-24  |  10KB  |  252 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                       GNAT ncurses Binding Samples                       --
  4. --                                                                          --
  5. --                                 ncurses                                  --
  6. --                                                                          --
  7. --                                 B O D Y                                  --
  8. --                                                                          --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000 Free Software Foundation, Inc.                        --
  11. --                                                                          --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a  --
  13. -- copy of this software and associated documentation files (the            --
  14. -- "Software"), to deal in the Software without restriction, including      --
  15. -- without limitation the rights to use, copy, modify, merge, publish,      --
  16. -- distribute, distribute with modifications, sublicense, and/or sell       --
  17. -- copies of the Software, and to permit persons to whom the Software is    --
  18. -- furnished to do so, subject to the following conditions:                 --
  19. --                                                                          --
  20. -- The above copyright notice and this permission notice shall be included  --
  21. -- in all copies or substantial portions of the Software.                   --
  22. --                                                                          --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
  30. --                                                                          --
  31. -- Except as contained in this notice, the name(s) of the above copyright   --
  32. -- holders shall not be used in advertising or otherwise to promote the     --
  33. -- sale, use or other dealings in this Software without prior written       --
  34. -- authorization.                                                           --
  35. ------------------------------------------------------------------------------
  36. --  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. --  Version Control
  38. --  $Revision: 1.1 $
  39. --  Binding Version 01.00
  40. ------------------------------------------------------------------------------
  41. --  Character input test
  42. --  test the keypad feature
  43.  
  44. with ncurses2.util; use ncurses2.util;
  45.  
  46. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  47. with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
  48. with Ada.Characters.Handling;
  49. with Ada.Strings.Bounded;
  50.  
  51. with ncurses2.genericPuts;
  52.  
  53. procedure ncurses2.getch_test is
  54.    use Int_IO;
  55.  
  56.    function mouse_decode (ep : Mouse_Event) return String;
  57.  
  58.    function mouse_decode (ep : Mouse_Event) return String is
  59.       Y      : Line_Position;
  60.       X      : Column_Position;
  61.       Button : Mouse_Button;
  62.       State  : Button_State;
  63.       package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
  64.       use BS;
  65.       buf : Bounded_String := To_Bounded_String ("");
  66.    begin
  67.       --  Note that these bindings do not allow
  68.       --  two button states,
  69.       --  The C version can print {click-1, click-3} for example.
  70.       --  They also don't have the 'id' or z coordinate.
  71.       Get_Event (ep, Y, X, Button, State);
  72.  
  73.       --  TODO Append (buf, "id "); from C version
  74.       Append (buf, "at (");
  75.       Append (buf, Column_Position'Image (X));
  76.       Append (buf, ", ");
  77.       Append (buf, Line_Position'Image (Y));
  78.       Append (buf, ") state");
  79.       Append (buf, Mouse_Button'Image (Button));
  80.  
  81.       Append (buf, " = ");
  82.       Append (buf, Button_State'Image (State));
  83.       return To_String (buf);
  84.    end mouse_decode;
  85.  
  86.  
  87.    buf : String (1 .. 1024); --  TODO was BUFSIZE
  88.    n : Integer;
  89.    c : Key_Code;
  90.    blockflag : Timeout_Mode := Blocking;
  91.    firsttime : Boolean := True;
  92.    tmp2  : Event_Mask;
  93.    tmp6 : String (1 .. 6);
  94.    tmp20 : String (1 .. 20);
  95.    x : Column_Position;
  96.    y : Line_Position;
  97.    tmpx : Integer;
  98.    incount : Integer := 0;
  99. begin
  100.    Refresh;
  101.    tmp2 := Start_Mouse (All_Events);
  102.    Add (Str => "Delay in 10ths of a second (<CR> for blocking input)? ");
  103.    Set_Echo_Mode (SwitchOn => True);
  104.    Get (Str => buf);
  105.  
  106.    Set_Echo_Mode (SwitchOn => False);
  107.    Set_NL_Mode (SwitchOn => False);
  108.  
  109.    if Ada.Characters.Handling.Is_Digit (buf (1)) then
  110.       Get (Item => n, From => buf, Last => tmpx);
  111.       Set_Timeout_Mode (Mode => Delayed, Amount => n * 100);
  112.       blockflag := Delayed;
  113.    end if;
  114.  
  115.    c := Character'Pos ('?');
  116.    Set_Raw_Mode (SwitchOn => True);
  117.    loop
  118.       if not firsttime then
  119.          Add (Str => "Key pressed: ");
  120.          Put (tmp6, Integer (c), 8);
  121.          Add (Str => tmp6);
  122.          Add (Ch => ' ');
  123.          if c = Key_Mouse then declare
  124.             event : Mouse_Event;
  125.          begin
  126.             event := Get_Mouse;
  127.             Add (Str => "KEY_MOUSE, ");
  128.             Add (Str => mouse_decode (event));
  129.             Add (Ch => newl);
  130.          end;
  131.          elsif c >= Key_Min then
  132.             Key_Name (c, tmp20);
  133.             Add (Str => tmp20);
  134.             --  I used tmp and got bitten by the length problem:->
  135.             Add (Ch => newl);
  136.          elsif c > 16#80# then --  TODO fix, use constant if possible
  137.             declare
  138.                c2 : Character := Character'Val (c mod 16#80#);
  139.             begin
  140.                if Ada.Characters.Handling.Is_Graphic (c2) then
  141.                   Add (Str => "M-");
  142.                   Add (Ch => c2);
  143.                else
  144.                   Add (Str => "M-");
  145.                   Add (Str => Un_Control ((Ch => c2,
  146.                                            Color => Color_Pair'First,
  147.                                            Attr => Normal_Video)));
  148.                end if;
  149.                Add (Str => " (high-half character)");
  150.                Add (Ch => newl);
  151.             end;
  152.          else declare
  153.             c2 : Character := Character'Val (c mod 16#80#);
  154.          begin
  155.             if Ada.Characters.Handling.Is_Graphic (c2) then
  156.                Add (Ch => c2);
  157.                Add (Str => " (ASCII printable character)");
  158.                Add (Ch => newl);
  159.             else
  160.                Add (Str => Un_Control ((Ch => c2,
  161.                                        Color => Color_Pair'First,
  162.                                        Attr => Normal_Video)));
  163.                Add (Str => " (ASCII control character)");
  164.                Add (Ch => newl);
  165.             end if;
  166.          end;
  167.          end if;
  168.          --  TODO I am not sure why this was in the C version
  169.          --  the delay statement scroll anyway.
  170.          Get_Cursor_Position (Line => y, Column => x);
  171.          if y >= Lines - 1 then
  172.             Move_Cursor (Line => 0, Column => 0);
  173.          end if;
  174.          Clear_To_End_Of_Line;
  175.       end if;
  176.  
  177.       firsttime := False;
  178.       if c = Character'Pos ('g') then
  179.          declare
  180.             package p is new ncurses2.genericPuts (1024);
  181.             use p;
  182.             use p.BS;
  183.             timedout : Boolean := False;
  184.             boundedbuf : Bounded_String;
  185.          begin
  186.             Add (Str => "getstr test: ");
  187.             Set_Echo_Mode (SwitchOn => True);
  188.             --  Note that if delay mode is set
  189.             --  Get can raise an exception.
  190.             --  The C version would print the string it had so far
  191.             --  also TODO get longer length string, like the C version
  192.             declare begin
  193.                myGet (Str => boundedbuf);
  194.             exception when Curses_Exception =>
  195.                Add (Str => "Timed out.");
  196.                Add (Ch => newl);
  197.                timedout := True;
  198.             end;
  199.             --  note that the Ada Get will stop reading at 1024.
  200.             if not timedout then
  201.                Set_Echo_Mode (SwitchOn => False);
  202.                Add (Str => " I saw '");
  203.                myAdd (Str => boundedbuf);
  204.                Add (Str => "'.");
  205.                Add (ch => newl);
  206.             end if;
  207.          end;
  208.       elsif c = Character'Pos ('s') then
  209.          ShellOut (True);
  210.       elsif c = Character'Pos ('x') or c = Character'Pos ('q') or
  211.         (c = Key_None and blockflag = Blocking) then
  212.          exit;
  213.       elsif c = Character'Pos ('?') then
  214.          Add (Str => "Type any key to see its keypad value.  Also:");
  215.          Add (Ch => newl);
  216.          Add (Str => "g -- triggers a getstr test");
  217.          Add (Ch => newl);
  218.          Add (Str => "s -- shell out");
  219.          Add (Ch => newl);
  220.          Add (Str => "q -- quit");
  221.          Add (Ch => newl);
  222.          Add (Str => "? -- repeats this help message");
  223.          Add (Ch => newl);
  224.       end if;
  225.  
  226.       loop
  227.          c := Getchar;
  228.          exit when c /= Key_None;
  229.          if blockflag /= Blocking then
  230.             Put (tmp6, incount); --  argh string length!
  231.             Add (Str => tmp6);
  232.             Add (Str => ": input timed out");
  233.             Add (Ch => newl);
  234.          else
  235.             Put (tmp6, incount);
  236.             Add (Str => tmp6);
  237.             Add (Str => ": input error");
  238.             Add (Ch => newl);
  239.             exit;
  240.          end if;
  241.          incount := incount + 1;
  242.       end loop;
  243.    end loop;
  244.  
  245.    tmp2 := Start_Mouse (No_Events);
  246.    Set_Timeout_Mode (Mode => Blocking, Amount => 0); --  amount is ignored
  247.    Set_Raw_Mode (SwitchOn => False);
  248.    Set_NL_Mode (SwitchOn => True);
  249.    Erase;
  250.    End_Windows;
  251. end ncurses2.getch_test;
  252.